package com.example.list;

import android.app.Activity;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;


public class BasePopwin extends PopupWindow {

    Activity context;

    public BasePopwin(Activity context, View contentView) {
        this(context, contentView,
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
    }

    public BasePopwin(Activity context, View contentView, int width, int height) {
        super(contentView, width, height);
        this.context = context;

        setBackgroundAlpha(0.5f);
        setOutsideTouchable(true);
        showAtLocation(contentView, Gravity.BOTTOM, 0, 0);


        setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    dismiss();
                    setBackgroundAlpha(1);
                }
                return false;
            }
        });

    }


    /**
     * 设置popupwindow外面背景透明度
     *
     * @param bgalpha 透明度  0-1   0-透明   1-不透明
     */
    public void setBackgroundAlpha(float bgalpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgalpha;
        context.getWindow().setAttributes(lp);
    }

}
